#include "ch8001.h"

void ch_set_vol(struct ch8001Stu *ch, uint8_t mem, uint8_t vol){
    ch->chSendBuffer[0] = 0x7E;                     // 包头
    ch->chSendBuffer[1] = 0x16;
    ch->chSendBuffer[2] = 0x00;
    ch->chSendBuffer[3] = 0x02;
    ch->chSendBuffer[4] = mem>0 ? 1 : 0;
    ch->chSendBuffer[5] = vol>30 ? 30 : vol;
    ch->chSendBuffer[6] = 0xEF;
    
    HAL_UART_Transmit_DMA(ch->huart, ch->chSendBuffer, 7);
}

void ch_Init(struct ch8001Stu *ch){
    HAL_Delay(700);
    // HAL_Delay(200);
    // ch_set_vol(ch, 1, 30);
}

void ch_play(struct ch8001Stu *ch, uint16_t index){
    ch->chSendBuffer[0] = 0x7E;                     // 包头
    ch->chSendBuffer[1] = 0x13;                     // 指定曲目
    ch->chSendBuffer[2] = 0x00;                     // cmd 长度高字节
    ch->chSendBuffer[3] = 0x02;                     // cmd 长度低字节
    ch->chSendBuffer[4] = index>>8;                 // 音频编号高字节
    ch->chSendBuffer[5] = (uint8_t)(index&0x00FF);    // 音频编号低字节
    ch->chSendBuffer[6] = 0xEF;                     // 结束符

    HAL_UART_Transmit_DMA(ch->huart, ch->chSendBuffer, 7);
}

void ch_play_folder(struct ch8001Stu *ch, uint8_t folderIndex, uint8_t audioIndex){
    ch->chSendBuffer[0] = 0x7E;                                 // 包头
    ch->chSendBuffer[1] = 0x1F;                                 // 指定文件夹
    ch->chSendBuffer[2] = 0x00;                                 // cmd 长度高字节
    ch->chSendBuffer[3] = 0x02;                                 // cmd 长度低字节
    ch->chSendBuffer[4] = folderIndex>99 ? 99 : folderIndex;    // 文件夹编号
    ch->chSendBuffer[5] = audioIndex;                           // 音频编号
    ch->chSendBuffer[6] = 0xEF;                                 // 结束符

    HAL_UART_Transmit_DMA(ch->huart, ch->chSendBuffer, 7);
}

void ch_sleep(struct ch8001Stu *ch){
    const uint8_t sleep[] = {0x7E,0x1A,0x00,0x02,0x00,0x01,0xEF};
    HAL_UART_Transmit_DMA(ch->huart, sleep, 7);
}
